package mi.payment.ui.admin;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import mi.payment.BuildConfig;
import mi.payment.R;
import mi.payment.controller.BaseTerminalKeyManagementController;
import mi.payment.controller.MasterImportKeyController;
import mi.payment.controller.TerminalKeyManagementController;
import mi.payment.interfaces.ITaskListener.DefaultTaskListener;
import mi.payment.settings.TerminalKeyDataOperations;
import mi.payment.settings.TerminalKeyManagementBean;
import mi.payment.ui.base.ParentActivity;
import mi.payment.utils.MustParameterCalibration;
import sunmi.sunmiui.title.Title;

/**
 * 终端密钥管理界面
 *
 * @author lj on 2017/1/7.
 */
public class TerminalKeyManagementActivity extends ParentActivity {

    private View terminalMasterKeyIndexView;
    private View terminalMasterDownloadView;
    private View terminalMasterInputView;
    private View setKekView;
    private View desAlgorithmsView;
    private View parentPosImportKeyView;
    private View ICParameterImportView;

    private TextView terminalMasterKeyIndexText;
    private TextView terminalMasterKeyInputText;
    private TextView kekText;
    private TextView desAlgorithmsText;
    private TextView ICParameterImportText;


    private TerminalKeyDataOperations terminalKeyDataOperations;
    private MasterImportKeyController masterImportKeyController;
    private BaseTerminalKeyManagementController controller;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initController();
        initView();
        initClickListener();
        updateUI();
    }

    private void initController() {
        terminalKeyDataOperations = TerminalKeyDataOperations.getInstance();
        masterImportKeyController = new MasterImportKeyController(this);
        masterImportKeyController.setListener(new DefaultTaskListener<String>() {

            @Override
            public void onTaskFailed(String ret) {
                showToast("保存成功");
            }

            @Override
            public void onTaskSuccess(String ret) {
                showToast("保存失败");
            }

        });
        controller = new TerminalKeyManagementController(this);
        controller.setTerminalKeyDataOperations(terminalKeyDataOperations);
    }

    public void initView() {
        setContentView(controller.getContentLayout());
        Title title = (Title) findViewById(R.id.terminal_key_management_title);
        title.setButtonVisibility(false, true, true, true);
        int color = getResources().getColor(R.color.colorBar);
        String str = getString(R.string.terminal_key_management);
        title.setHeadBgColor(color);
        title.setTitleText(str);
        title.setMenuClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }

        }, null, null, null);

        TextView itemLeftText;
        terminalMasterKeyIndexView = findViewById(R.id.terminal_master_key_index);
        terminalMasterDownloadView = findViewById(R.id.terminal_master_key_download);
        terminalMasterInputView = findViewById(R.id.terminal_master_key_input);
        setKekView = findViewById(R.id.set_kek);
        desAlgorithmsView = findViewById(R.id.des_algorithms);
        parentPosImportKeyView = findViewById(R.id.parent_pos_import_key);
        ICParameterImportView = findViewById(R.id.ic_params_download);

        terminalMasterKeyIndexText = (TextView) terminalMasterKeyIndexView.findViewById(R.id.item_right_text);
        terminalMasterKeyInputText = (TextView) terminalMasterInputView.findViewById(R.id.item_right_text);
        kekText = (TextView) setKekView.findViewById(R.id.item_right_text);
        desAlgorithmsText = (TextView) desAlgorithmsView.findViewById(R.id.item_right_text);
        ICParameterImportText = (TextView) ICParameterImportView.findViewById(R.id.item_right_text);
        // 设置主密钥索引
        itemLeftText = (TextView) terminalMasterKeyIndexView.findViewById(R.id.item_left_text);
        itemLeftText.setText(R.string.terminal_master_key_index);

        // 终端主密钥下载
        itemLeftText = (TextView) terminalMasterDownloadView.findViewById(R.id.item_left_text);
        switch (BuildConfig.FLAVOR) {
            case "lePos": // 乐刷
                itemLeftText.setText("激活POS设备");
                break;
            default:
                itemLeftText.setText(R.string.TMK_download);
                break;
        }

        // 输入终端主密钥
        itemLeftText = (TextView) terminalMasterInputView.findViewById(R.id.item_left_text);
        itemLeftText.setText(R.string.terminal_master_key_input);

        // 设置KEK
        itemLeftText = (TextView) setKekView.findViewById(R.id.item_left_text);
        itemLeftText.setText(R.string.terminal_key_set_kek);

        // 设置DES算法
        itemLeftText = (TextView) desAlgorithmsView.findViewById(R.id.item_left_text);
        itemLeftText.setText(getString(R.string.terminal_key_des_algorithms));

        // 母POS导入
        itemLeftText = (TextView) parentPosImportKeyView.findViewById(R.id.item_left_text);
        itemLeftText.setText(R.string.terminal_key_parent_pos_import_key);

        // IC卡导入密钥
        itemLeftText = (TextView) ICParameterImportView.findViewById(R.id.item_left_text);
        itemLeftText.setText(R.string.terminal_key_ic_parameter_import);

        terminalMasterKeyIndexText.setText(R.string.terminal_key_master_key_hint);
        terminalMasterKeyInputText.setText(R.string.terminal_key_master_key_input_hint);
        kekText.setText(R.string.terminal_key_set_kek_hint);
        desAlgorithmsText.setText(R.string.terminal_key_des_algorithms_hint);
        ICParameterImportText.setText(R.string.terminal_key_ic_parameter_import_hint);

        boolean b = controller.isHideTerminalMasterKeyIndex();
        if (b) {
            terminalMasterKeyIndexView.setVisibility(View.GONE);
        }
        b = controller.isHideTerminalMasterKeyDownload();
        if (b) {
            terminalMasterDownloadView.setVisibility(View.GONE);
        }
        b = controller.isShowTerminalMasterKeyInput();
        if (b) {
            terminalMasterInputView.setVisibility(View.GONE);
        }
        b = controller.isHideSetKEK();
        if (b) {
            setKekView.setVisibility(View.GONE);
        }
        if (controller.isHideDesAlgorithms()) {
            desAlgorithmsView.setVisibility(View.GONE);
        }
        b = controller.isHideParentPosImportKey();
        if (b) {
            parentPosImportKeyView.setVisibility(View.GONE);
        }
        b = controller.isHideICParameterImport();
        if (b) {
            ICParameterImportView.setVisibility(View.GONE);
        }
        controller.init();
    }

    private void initClickListener() {
        terminalMasterKeyIndexView.setOnClickListener(this);
        terminalMasterDownloadView.setOnClickListener(this);
        terminalMasterInputView.setOnClickListener(this);
        setKekView.setOnClickListener(this);
        desAlgorithmsView.setOnClickListener(this);
        parentPosImportKeyView.setOnClickListener(this);
        ICParameterImportView.setOnClickListener(this);
    }

    public void updateUI() {
        TerminalKeyManagementBean bean = terminalKeyDataOperations.getTerminalKeyManagementBean();
        String keyIndex = bean.getTerminalKeyIndex();
        if ( TextUtils.isEmpty(keyIndex) ) {
            terminalMasterKeyIndexText.setText(R.string.terminal_key_master_key_hint);
        } else {
            terminalMasterKeyIndexText.setText(keyIndex);
        }

        String des = bean.getDes();
        if ( TextUtils.isEmpty(des) ) {
            desAlgorithmsText.setText(R.string.terminal_key_des_algorithms_hint);
        } else if ( des.equals(TerminalKeyManagementBean.SINGLE_DES) ) {
            desAlgorithmsText.setText(R.string.single_des);
        } else if ( des.equals(TerminalKeyManagementBean.THREE_DES) ) {
            desAlgorithmsText.setText(R.string.three_des);
        }

        int icImportKey = bean.getIcImportKey();
        if (icImportKey == TerminalKeyManagementBean.IC_IMPORT_KEY1) {
            ICParameterImportText.setText(R.string.tongyong);
        } else if (icImportKey == TerminalKeyManagementBean.IC_IMPORT_KEY2) {
            ICParameterImportText.setText(R.string.shenyinlian);
        } else {
            ICParameterImportText.setText(R.string.terminal_key_ic_parameter_import_hint);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.terminal_master_key_index: // 终端主密钥索引
                controller.setTerminalMasterKeyIndex();
                break;
            case R.id.terminal_master_key_download: // TMK下载
                boolean ipAndPort = MustParameterCalibration.isNullServerIPAndPort();
                if (ipAndPort) {
                    showToast(R.string.system_errorcode);
                } else {
                    controller.downloadTMK();
                }
                break;
            case R.id.terminal_master_key_input: // 手输TMK
                controller.setTerminalMasterKey();
                break;
            case R.id.set_kek: // 手输KEK
                controller.downloadKEK();
                break;
            case R.id.des_algorithms: // DES算法
                controller.showDesAlgorithmsDialog();
                break;
            case R.id.parent_pos_import_key: // 母POS导入密钥
                masterImportKeyController.start();
                break;
            case R.id.ic_params_download: // IC卡导入密钥
                controller.ICParameterImport();
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        masterImportKeyController.applySerial();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        masterImportKeyController.stop();
        controller.onDestroy();
    }


}
